package com.ruoyi.tianliangedu.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.tianliangedu.domain.TianliangFatherSection;
import com.ruoyi.tianliangedu.domain.TianliangSonSection;
import com.ruoyi.tianliangedu.service.ITianliangFatherSectionService;
import com.ruoyi.tianliangedu.service.ITianliangSonSectionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 子类章节Controller
 *
 * @author lsj
 * @date 2021-07-12
 */
@Controller
@RequestMapping("/tianliangedu/sonSection")
public class TianliangSonSectionController extends BaseController {
    private String prefix = "tianliangedu/sonSection";

    @Autowired
    private ITianliangSonSectionService tianliangSonSectionService;

    @Autowired
    private ITianliangFatherSectionService tianliangFatherSectionService;

    @RequiresPermissions("tianliangedu:sonSection:view")
    @GetMapping()
    public String sonSection() {
        return prefix + "/sonSection";
    }

    /**
     * 查询子类章节列表
     */
    @RequiresPermissions("tianliangedu:sonSection:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TianliangSonSection tianliangSonSection) {
        startPage();
        List<TianliangSonSection> list = tianliangSonSectionService.selectTianliangSonSectionList(tianliangSonSection);
        return getDataTable(list);
    }

    /**
     * 导出子类章节列表
     */
    @RequiresPermissions("tianliangedu:sonSection:export")
    @Log(title = "子类章节", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TianliangSonSection tianliangSonSection) {
        List<TianliangSonSection> list = tianliangSonSectionService.selectTianliangSonSectionList(tianliangSonSection);
        ExcelUtil<TianliangSonSection> util = new ExcelUtil<TianliangSonSection>(TianliangSonSection.class);
        return util.exportExcel(list, "子类章节数据");
    }

    /**
     * 新增子类章节
     */
    @GetMapping("/add")
    public String add(ModelMap map) {
        TianliangFatherSection tianliangFatherSection = new TianliangFatherSection();
        List<TianliangFatherSection> list = tianliangFatherSectionService.selectTianliangFatherSectionList(tianliangFatherSection);
        map.put("listName", list);
        return prefix + "/add";
    }

    /**
     * 新增保存子类章节
     */
    @RequiresPermissions("tianliangedu:sonSection:add")
    @Log(title = "子类章节", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TianliangSonSection tianliangSonSection) {
        return toAjax(tianliangSonSectionService.insertTianliangSonSection(tianliangSonSection));
    }

    /**
     * 修改子类章节
     */
    @GetMapping("/edit/{sonSectionId}")
    public String edit(@PathVariable("sonSectionId") Long sonSectionId, ModelMap mmap) {
        TianliangSonSection tianliangSonSection = tianliangSonSectionService.selectTianliangSonSectionById(sonSectionId);
        mmap.put("tianliangSonSection", tianliangSonSection);
        return prefix + "/edit";
    }

    /**
     * 修改保存子类章节
     */
    @RequiresPermissions("tianliangedu:sonSection:edit")
    @Log(title = "子类章节", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TianliangSonSection tianliangSonSection) {
        return toAjax(tianliangSonSectionService.updateTianliangSonSection(tianliangSonSection));
    }

    /**
     * 删除子类章节
     */
    @RequiresPermissions("tianliangedu:sonSection:remove")
    @Log(title = "子类章节", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(tianliangSonSectionService.deleteTianliangSonSectionByIds(ids));
    }
}
